﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TallyMarks.Calculators;
using TallyMarks.Models;

namespace TallyMarks
{
    public partial class MainPage : PhoneApplicationPage
    {
        private readonly LayoutCalculator _layoutCalculator;

        private static TallyModel Model
        {
            get
            {
                return ((App)Application.Current).Model;
            }
        }

        public MainPage()
        {
            InitializeComponent();
            SupportedOrientations = SupportedPageOrientation.Portrait;
            _layoutCalculator = new LayoutCalculator();
            DataContext = Model;
            Model.PropertyChanged += Model_PropertyChanged;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            DisplayMarks();
        }

        private void DisplayMarks()
        {
            MarksPanel.Children.Clear();
            if (Model.Count == 0) DisplayStartTip();
            var layoutMath = _layoutCalculator.GetLayout(Model.Count);
            AddTallyControls(layoutMath);
        }

        private void DisplayStartTip()
        {
            MarksPanel.Children.Add(new StartTipControl());
        }

        private void AddTallyControls(LayoutMath layout)
        {
            for (var i = 1; i <= layout.NumberofControlsNeeded; i++)
            {
                MarksPanel.Children.Add(
                    new MarksUserControl { PercentageOfActualSize = layout.SizeFactor });
            }
            SetLastTallyMarkToState(Model.Count - layout.NumberOfMarksToRemoveFromLastControl);
            if (layout.ScreenFull) TallyScrollViewer.ScrollToBottom();
        }

        private void SetLastTallyMarkToState(double count)
        {
            var state = "Tally" + count;
            var marks = MarksPanel.Children[MarksPanel.Children.Count - 1] as Control;
            VisualStateManager.GoToState(marks, state, false);
        }

        private void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Count")
            {
                DisplayMarks();
                AdjustApplicationBar();
            }
        }

        private void AdjustApplicationBar()
        {
            var minus = (ApplicationBarIconButton) ApplicationBar.Buttons[1];
            var clear = (ApplicationBarIconButton)ApplicationBar.Buttons[2];
            minus.IsEnabled = clear.IsEnabled = (Model.Count > 0);
        }

        private void Add_Click(object sender, EventArgs e)
        {
            Model.Count++;
        }

        private void Remove_Click(object sender, EventArgs e)
        {
            Model.Count--;
        }

        private void Clear_Click(object sender, EventArgs e)
        {
            var result = MessageBox.Show("Clear your tally marks?", "", MessageBoxButton.OKCancel);
            if (result == MessageBoxResult.OK) Model.Count = 0;
        }
    }
}